package com.java2007.hotel.controller.front;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.controller.BaseServlet;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodService;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.utils.PageSupport;
import com.mysql.cj.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

/**
 * 处理查询前台菜的请求
 * @author csl
 * @version V1.0
 * @date 2021/01/25
 **/
@WebServlet("/front/foodServlet")
public class FrontFoodServlet extends BaseServlet {
    private FoodService foodService = (FoodService) BeanFactory.getBean(Constant.FOOD_SERVICE_IMPL);
    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean(Constant.FOOD_TYPE_SERVICE_IMPL);
    /**
     * 前端查询所有菜
     * @param req
     * @param resp
     * @return
     */
    public String selectAll(HttpServletRequest req, HttpServletResponse resp){
        //保存桌子
        String foodTableId = req.getParameter("foodTableId");
        HttpSession session = req.getSession();
        if (foodTableId == null){
            foodTableId =  session.getAttribute("foodTableId")+"";
        }
        req.getSession().setAttribute("foodTableId",Integer.parseInt(foodTableId));

        List<Food> foods = null;
        try {
            foods = foodService.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        int totalCount = foods.size();
        String tempCurrentPageNo = req.getParameter("currentPageNo");
        int currentPageNo = Integer.parseInt(tempCurrentPageNo);
        PageSupport pageSupport = new PageSupport(totalCount,currentPageNo);
        int startCount = pageSupport.getStartCount();
        int totalPageCount = pageSupport.getTotalPageCount();
        int pageSize = pageSupport.getPageSize();


        List<Food> foods1 = null;
        try {
            foods1 = foodService.selectLimit(startCount, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        //查询菜系
        List<FoodType> foodTypes = null;
        try {
            foodTypes = foodTypeService.selectAllFoodType();
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        req.setAttribute("foodTypes",foodTypes);
        req.setAttribute("frontFoods",foods1);
        req.setAttribute("totalPageCount",totalPageCount);
        req.setAttribute("currentPageNo",pageSupport.getCurrentPageNo());

        req.setAttribute("navigation","selectAll");
        return "forward:"+"detail/caidan.jsp";

    }


    /**
     * 根据菜系查询cai
     * @param req
     * @param resp
     * @return
     */
    public String selectByFoodType(HttpServletRequest req, HttpServletResponse resp){
        String tempFoodTypeId = req.getParameter("foodTypeId");
        int foodTypeId = Integer.parseInt(tempFoodTypeId);
        List<Food> foods = null;
        try {
            foods = foodService.selectFoodBy(foodTypeId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        int totalCount = foods.size();
        String tempCurrentPageNo = req.getParameter("currentPageNo");
        int currentPageNo = Integer.parseInt(tempCurrentPageNo);
        PageSupport pageSupport = new PageSupport(totalCount,currentPageNo);
        int startCount = pageSupport.getStartCount();
        int totalPageCount = pageSupport.getTotalPageCount();
        int pageSize = pageSupport.getPageSize();


        List<Food> foods1 = null;
        try {
            foods1 = foodService.selectFoodBy(foodTypeId, startCount, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }


        //查询菜系
        List<FoodType> foodTypes = null;
        try {
            foodTypes = foodTypeService.selectAllFoodType();
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        req.setAttribute("foodTypes",foodTypes);
        req.setAttribute("frontFoods",foods1);
        req.setAttribute("totalPageCount",totalPageCount);
        req.setAttribute("currentPageNo",pageSupport.getCurrentPageNo());

        req.setAttribute("foodTypeId",foodTypeId);

        req.setAttribute("navigation","selectByFoodType");
        return "forward:"+"detail/caidan.jsp";
    }

    /**
     * 根据名字模糊分页查询
     * @param req
     * @param resp
     * @return
     */
    public String selectFood(HttpServletRequest req, HttpServletResponse resp){
        String foodName = req.getParameter("foodName");
        List<Food> foods = null;
        try {
            foods = foodService.selectFoodByName(foodName);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        int totalCount = foods.size();
        String tempCurrentPageNo = req.getParameter("currentPageNo");
        int currentPageNo = Integer.parseInt(tempCurrentPageNo);
        PageSupport pageSupport = new PageSupport(totalCount,currentPageNo);
        int startCount = pageSupport.getStartCount();
        int totalPageCount = pageSupport.getTotalPageCount();
        int pageSize = pageSupport.getPageSize();


        List<Food> foods1 = null;
        try {
            foods1 = foodService.selectFoodByNameLimit(foodName,startCount,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }


        //查询菜系
        List<FoodType> foodTypes = null;
        try {
            foodTypes = foodTypeService.selectAllFoodType();
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        req.setAttribute("foodTypes",foodTypes);
        req.setAttribute("frontFoods",foods1);
        req.setAttribute("totalPageCount",totalPageCount);
        req.setAttribute("currentPageNo",pageSupport.getCurrentPageNo());

        req.setAttribute("foodName",foodName);

        req.setAttribute("navigation","selectFood");
        return "forward:"+"detail/caidan.jsp";
    }

    /**
     * 查询Food
     * @param req
     * @param resp
     * @return
     */
    public String xiangFood(HttpServletRequest req, HttpServletResponse resp){
        String tempCiadanFoodId = req.getParameter("ciadanFoodId");
        int i = Integer.parseInt(tempCiadanFoodId);
        Food food = null;
        try {
            food = foodService.selectFoodById(i);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        req.setAttribute("caixaing",food);
        //查询菜系
        List<FoodType> foodTypes = null;
        try {
            foodTypes = foodTypeService.selectAllFoodType();
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        req.setAttribute("foodTypes",foodTypes);
        return "forward:"+"detail/caixiangxi.jsp";
    }




}
